/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

import com.core.log.Debug;
import com.core.utils.Utils;
import com.server.database.DataEngine;
import com.server.database.UserInfo;
import com.server.main.ClientManager;

/**
 *
 * @author daz
 */
public class GamePlayer {
    
    public ClientManager parent;
    private GameTable table;
    private UserInfo userInfo;
    private String name;
    private long userId;
    private int readyState;
    public int score, sttHaBai;
    private int playerId;
    private int serverId;
    private int roomId;
    
    public GamePlayer(ClientManager parent) {
        this.parent = parent;
        this.userId = parent.getUserId();
        this.name = parent.getUserName();
        this.userInfo = parent.getUserInfo();
        parent.setPlayer(this);
        roomId = -1;
    }
    
    public void setRooomId(int roomId) {
        this.roomId = roomId;
    }
    
    public void resetTimeDb() {
        parent.resetTimeDb();
    }
    
    public int getRooomId() {
        return this.roomId;
    }
    
    public int getReadyState() {
        return this.readyState;
    }
    
    public int getAvatarId() {
        return parent.getAvatarId();
    }
    
    public void setReadyState(int readyState) {
        this.readyState = readyState;
    }
    
    public void updateLevel(int newLevel) {
        this.parent.updateLevel(newLevel);
    }
    
    public void updateWin() {
        int newWin = userInfo.getNumberWin() + 1;
        int currentLevel = userInfo.getLevel();
        if (newWin >= Utils.fibonaci(currentLevel + 1)) {
            updateLevel(currentLevel + 1);
        }
        this.parent.updateWin(newWin);
    }
    
    public void updateLose() {
        int newLose = userInfo.getNumberLose() + 1;
        this.parent.updateLose(newLose);
    }
    
    public void sendMessage(String msg) {
        parent.sendMessage(msg);
    }
    
    public long getMoney() {
        return parent.getUserMoney();
    }
    
    public int getUserLevel() {
        return userInfo.getLevel();
    }
    
    public void setMoney(int money) {
        this.userInfo.setMoney(money);
    }
    
    public void setDeltaMoney(long money) {
        long newMoney = this.userInfo.getMoney();
        newMoney += money;
        this.userInfo.setMoney(newMoney);
    }
    
    public int getGender() {
        return this.userInfo.getGender();
    }
    
    public void setTable(GameTable table, int id) {
        this.table = table;
        this.playerId = id;
        parent.setGameTable(table);
        parent.setPlayerId(id);
    }
    
    public String getUserName() {
        return this.name;
    }
    
    public void updateMoneyToDatabase(long numberMoney) {
        parent.updateMoneyToDataBase(numberMoney);
    }
    
    public void updateMoneyDelta(long deltaMoney) {
        parent.updateMoneyDelta(deltaMoney);
    }
    
    public long getUserId() {
        return userId;
    }
    
    public boolean response(String message) {
        return parent.sendMessage(message);
    }
    
    public void setStartTimeDb(int numberPlays) {
        parent.setStartTimeDb(numberPlays);
    }
    
    public int getPlayerId() {
        return this.playerId;
    }
    
    int getItemType() {
        return parent.getItemUse();
    }
    
    public int getCompanyId() {
        if (this.userInfo == null) {
            this.userInfo = parent.getUserInfo();
        }
        return this.userInfo.getCompanyId();
    }
    
    public UserInfo getUserInfo() {
        if (this.userInfo == null) {
            this.userInfo = parent.getUserInfo();
        }
        return userInfo;
    }
}
